//计算百分比
function GetPercent(num, total) {
    num = parseFloat(num);
    total = parseFloat(total);
    if (isNaN(num) || isNaN(total)) {
        return "-";
    }
    return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.0) + "%";
};
//获取当前年份
var date = new Date;
var year = date.getFullYear();
var data = [
    year, year - 1, year - 2
];
var optionstring = "";
for (var i = 0; i < data.length; i++) {
    if (data[i] == year) {
        optionstring += "<option selected = \"selected\"  value=" + data[i] + ">" + data[i] + "年" + "</option>"; //公共变量赋值
    } else {
        optionstring += "<option value=" + data[i] + ">" + data[i] + "年" + "</option>"; //公共变量赋值
    }
}
;
$("#year").html(optionstring);   //下拉框加载数据
var tableData = [];
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        tableData: []
    },
    mounted: function () {
        // this.getNewsTotal();
        this.datas();
    },
    methods: {
        datas: function () {
            var tada = [];
            var _this = this;
            console.log($("#year").val());
            $.getJSON(baseURL + 'generator/projectentity/projectAnalysis/' + $("#year").val(), function (r) {
                var selectYear = $("#year").val();
                var sourceData = r.data.phase;
                var totalP = r.data.projectCount;
                var overdueMap = r.data.overdueMap;
                for (var i = 0; i < sourceData.length; i++) {
                    var temp = {
                        'projectYear': $("#year").val() + '年',
                        'phaseName': sourceData[i].phaseName,
                        'projectCount': totalP,
                        'overdueNum': overdueMap[sourceData[i]['phaseName']],
                        'overdueProportion': GetPercent(overdueMap[sourceData[i]['phaseName']], totalP)
                    };
                    tada.push(temp);
                }

                var xAxisData = [];
                var noOverdueMapData = [];
                var overdueMapData = [];
                for (var key in r.data.noOverdueMap) {
                    xAxisData.push(key);
                    noOverdueMapData.push(r.data.noOverdueMap[key]);
                }
                for (var i in r.data.overdueMap) {
                    overdueMapData.push(r.data.overdueMap[i]);
                }
                var axisData = [];
                for (var k = 0; k < xAxisData.length; k++) {
                    for (var i = 0; i < tada.length; i++) {
                        if (xAxisData[k] == tada[i].phaseName) {
                            axisData[k] = tada[i];
                        }
                    }
                }
                _this.tableData = axisData;
                // 基于准备好的dom，初始化echarts实例
                var myChart = echarts.init(document.getElementById('main'));
                // 指定图表的配置项和数据
                option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    legend: {
                        data: ['超时', '未超时']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xAxisData,
                            axisLabel: {
                                interval: 0
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: [
                        {
                            name: '超时',
                            type: 'bar',
                            data: overdueMapData
                        },
                        {
                            name: '未超时',
                            type: 'bar',
                            data: noOverdueMapData
                        }
                    ]
                };

                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);
            })
        }/*,
        getNewsTotal: function () {
            // 获取新闻信息数据
            var _this = this;
            console.log(_this.tableData);
            _this.tableData = tableData;

        }*/
    }
});
